<template>
  <div>
    <el-card class="box-card">
      <el-button type="primary" @click="addCard">添加分类</el-button>
      <el-table :data="cateData" style="width: 100%" border stripe  :cell-style="{padding:'0'}" :row-style="{padding:'0'}" highlight-current-row v-loading>
        <el-table-column type="expand" >
        <template #default="scope">
          <el-row v-for="(item1,i1) in scope.row.children" :key="item1.cat_id" class="vcenter" justify="center">
            <el-col :span="8" :offset="2">
              <el-tag>{{ item1.cat_name }}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <el-col :span="14">
                  <el-tag class="elTag" type="success" v-for="item2 in item1.children" :key="item2.cat_id">{{ item2.cat_name }}</el-tag>
            </el-col>
          </el-row>
        </template>
        </el-table-column>
        <el-table-column label="分类名称" prop="cat_name"  align="center">
        </el-table-column>
        <el-table-column label="是否有效" prop="cat_deleted" align="center">
          <template #default="scope">
            <i class="el-icon-success" style="color: limegreen" v-if="scope.row.cat_deleted === true"></i>
            <i class="el-icon-error" v-else style="color: sandybrown"></i>
          </template>
        </el-table-column >
        <el-table-column label="排序" prop="cat_level" align="center">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.cat_level === 0">等级一</el-tag>
            <el-tag type="info" v-else-if="scope.row.cat_level===1">等级二</el-tag>
            <el-tag type="warning" v-else-if="scope.row.cat_level === 2">等级三</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="cat_delete" align="center" >
          <template #default="scope">
            <el-button type="primary" @click="updateCard(scope.row)" size="medium">编辑</el-button>
            <el-button type="danger" @click="removeCard(scope.row)" size="medium">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--    分页区域-->
    <el-pagination  :current-page="params.pagenum" :page-size="params.pagesize"
                    :page-sizes="[2, 5, 8, 10]" :total="total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange">
    </el-pagination>
<!--    添加分类-->
    <DataChange ref="addDialog">

    </DataChange>
  </div>
</template>

<script>
import DataChange from "./DataChange";
import {request} from "../../../network/axios";

export default {
  name: "Card",
  created() {
    this.getCateData()
  },
  data(){
    return{
      cateData:[],
      params:{
        type:3,
        pagenum:1,
        pagesize:5
      },
      total:0
    }
  },
  methods:{
    getCateData(){
      request({
          url:`categories`,
        method:'get',
        params:this.params
      }).then(res=>{
        this.cateData = res.data.data.result
        console.log(res.data.data.result)
        this.total = res.data.data.total
      })
    },
    //选择每章多少条数据
    handleSizeChange(size){
      this.params.pagesize = size
      this.getCateData()
    },
    //选择第几页
    handleCurrentChange(page){
      this.params.pagenum = page
      this.getCateData()
    },
    //添加分类按钮功能
    addCard(){
        this.$refs.addDialog.addDialogVisible = !this.$refs.addDialog.addDialogVisible
    },
    //编辑按钮功能
    updateCard(val){
      this.$refs.addDialog.id = val.cat_id
      this.$refs.addDialog.changeDialogVisible = !this.$refs.addDialog.changeDialogVisible
    },
    //删除按钮功能
    removeCard(val){
      this.$refs.addDialog.id2 =val.cat_id
      this.$refs.addDialog.deleteData()
    }


  },
  components:{
    DataChange
  }
}
</script>

<style scoped src="../../../assets/css/goods/Card.css">

</style>